package com.xqls.controller;

import com.xqls.biz.*;
import com.xqls.dal.model.Remotecontrol;
import com.xqls.util.CommonPage;
import com.xqls.util.CommonResult;
import com.xqls.util.DictionaryEnums;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import java.util.List;
import java.util.Map;

@Controller
@RequestMapping(value = "/control")
public class DischargeRemoteController {
	@Autowired
	private DischargeRealdataBiz discharBiz;
	@Autowired
	private CompanyBiz companyBiz;
	@Autowired
	RainwaterDischargeRealdataBiz rainBiz;
	@Autowired
	private RemotecontrolBiz remoteBiz;
	@Autowired 
	private  PublicStormwaterRealdataBiz storwaterBiz;

	@Autowired private DictionaryBiz dictionaryBiz;

	
	@Autowired
	private FluggasDischargeRealdataBiz fluggasDischargeBiz;

    @Autowired
    private AdministrationBiz administrationBiz;
	@RequestMapping(value = "/dischargeRemote")
	public ModelAndView dischargeRemote() {
		ModelAndView view = new ModelAndView("RemoteList/dischargeRemote");
		List<Map<String,Object>> dischargeList=discharBiz.queryAll();
		for (int i=0;i<dischargeList.size();i++){
			dischargeList.get(i).put("viewId",i+1);

		}
        List<String> displayfiled = administrationBiz.conversion("discharge_realdata");
        boolean tap=true;
		c:for(int i=0;i<displayfiled.size();i++){

			if("valve_state".equals(displayfiled.get(i))){
                for(int j=0;j<displayfiled.size();j++) {
                    if("valve_setting".equals(displayfiled.get(j))) {
                        tap=false;
                        break c;
                    }
                }
			}else{
                tap=true;
			}
		}
        view.addObject("tap",tap);
		view.addObject("companyList", companyBiz.getList(DictionaryEnums.DISCHARGE));
		view.addObject("dischargeList", dischargeList);
		view.addObject("isRemoteList", true);
		Map<Integer, String> remoteTypeMap = dictionaryBiz.queryDictionaryByParent(
				 DictionaryEnums.REMOTE_TYPE.getCode());
		view.addObject("remoteTypeMap", remoteTypeMap);
		Map<Integer, String> startTypeMap = dictionaryBiz.queryDictionaryByParent(
				 DictionaryEnums.START_MODE.getCode());
		view.addObject("startTypeMap", startTypeMap);
		view.addObject("remote",remoteBiz.selectMaxTime("discharge") );
		Map<Integer, String> samplingTypeMap = dictionaryBiz.queryDictionaryByParent(
				 DictionaryEnums.CONCENTRATION_SAMPLING.getCode());
		view.addObject("samplingTypeMap", samplingTypeMap);
		
		return view;
	}

	@RequestMapping(value = "/radioDischargeRemote/{device_no}")
	public ModelAndView radiodischargeRemote(@PathVariable(value = "device_no") String device_no) {
		ModelAndView view = new ModelAndView("RemoteList/dischargeRemote");
        List<String> displayfiled = administrationBiz.conversion("discharge_realdata");
        boolean tap=true;
        c:for(int i=0;i<displayfiled.size();i++){

            if("valve_state".equals(displayfiled.get(i))){
                for(int j=0;j<displayfiled.size();j++) {
                    if("valve_setting".equals(displayfiled.get(j))) {
                        tap=false;
                        break c;
                    }else{
                        tap=true;
                    }
                }
            }else{
                tap=true;
            }
        }
        view.addObject("tap",tap);
		view.addObject("companyList", companyBiz.getList(DictionaryEnums.DISCHARGE));

		view.addObject("discharge", discharBiz.queryByDeviceNo(device_no));

		
		view.addObject("remote", remoteBiz.selectMaxTimeByDeviceNo(device_no, "discharge"));
		Map<Integer, String> remoteTypeMap = dictionaryBiz.queryDictionaryByParent(
				 DictionaryEnums.REMOTE_TYPE.getCode());
		view.addObject("remoteTypeMap", remoteTypeMap);
		Map<Integer, String> startTypeMap = dictionaryBiz.queryDictionaryByParent(
				 DictionaryEnums.START_MODE.getCode());
		view.addObject("startTypeMap", startTypeMap);
		Map<Integer, String> samplingTypeMap = dictionaryBiz.queryDictionaryByParent(
				 DictionaryEnums.CONCENTRATION_SAMPLING.getCode());
		view.addObject("samplingTypeMap", samplingTypeMap);
		return view;
	}

	@RequestMapping(value = "/rainwaterRemote")
	public ModelAndView rainwaterRemote() {
		ModelAndView view = new ModelAndView("RemoteList/rainwaterRemote");
		List<Map<String,Object>> rainwaterList=rainBiz.queryAll();
		for (int i=0;i<rainwaterList.size();i++){
			rainwaterList.get(i).put("viewId",i+1);
		}
		view.addObject("companyList", companyBiz.getList(DictionaryEnums.RAINWATER));
		view.addObject("rainwaterList", rainwaterList);
		view.addObject("isRemoteList", true);
		Map<Integer, String> valveTypeMap = dictionaryBiz.queryDictionaryByParent(
				 DictionaryEnums.VALVE_OPERATION.getCode());
		view.addObject("valveTypeMap", valveTypeMap);
		view.addObject("remote",remoteBiz.selectMaxTime("rainwater") );
		Map<Integer, String> samplingTypeMap = dictionaryBiz.queryDictionaryByParent(
				 DictionaryEnums.SAMPLING_MODE.getCode());
		view.addObject("samplingTypeMap", samplingTypeMap);
		return view;
	}

	@RequestMapping(value = "/radioRainwaterRemote/{device_no}")
	public ModelAndView radiorainwaterRemote(@PathVariable(value = "device_no") String device_no) {
		ModelAndView view = new ModelAndView("RemoteList/rainwaterRemote");
		view.addObject("companyList", companyBiz.getList(DictionaryEnums.RAINWATER));
		view.addObject("rainwater", rainBiz.selectByDeviceNo(device_no));
		
		view.addObject("remote", remoteBiz.selectMaxTimeByDeviceNo(device_no, "rainwater"));
		Map<Integer, String> valveTypeMap = dictionaryBiz.queryDictionaryByParent(
				 DictionaryEnums.VALVE_OPERATION.getCode());
		view.addObject("valveTypeMap", valveTypeMap);
		Map<Integer, String> samplingTypeMap = dictionaryBiz.queryDictionaryByParent(
				 DictionaryEnums.SAMPLING_MODE.getCode());
		view.addObject("samplingTypeMap", samplingTypeMap);
		return view;
	}
	
	
	@RequestMapping(value = "/stormwaterRemote")
	public ModelAndView stormwaterRemote() {
		ModelAndView view = new ModelAndView("RemoteList/stormwaterRemote");
		List<Map<String,Object>> stormwaterList=storwaterBiz.queryAll();
		for (int i=0;i<stormwaterList.size();i++){
			stormwaterList.get(i).put("viewId",i+1);
		}
		view.addObject("companyList", companyBiz.getList(DictionaryEnums.STORMWATER));
		view.addObject("stormwaterList", stormwaterList);
		view.addObject("isRemoteList", true);
		Map<Integer, String> startTypeMap = dictionaryBiz.queryDictionaryByParent(
				 DictionaryEnums.START_MODE.getCode());
		view.addObject("startTypeMap", startTypeMap);
		Map<Integer, String> remoteTypeMap = dictionaryBiz.queryDictionaryByParent(
				 DictionaryEnums.REMOTE_TYPE.getCode());
		view.addObject("remoteTypeMap", remoteTypeMap);
		view.addObject("remote",remoteBiz.selectMaxTime("stormwater") );
		Map<Integer, String> samplingTypeMap = dictionaryBiz.queryDictionaryByParent(
				 DictionaryEnums.CONCENTRATION_SAMPLING.getCode());
		view.addObject("samplingTypeMap", samplingTypeMap);
		return view;
	}

	@RequestMapping(value = "/radioStormwaterRemote/{device_no}")
	public ModelAndView radiostormwaterRemote(@PathVariable(value = "device_no") String device_no) {
		ModelAndView view = new ModelAndView("RemoteList/stormwaterRemote");
		view.addObject("companyList", companyBiz.getList(DictionaryEnums.STORMWATER));
		view.addObject("stormwater", storwaterBiz.selectByDeviceNo(device_no));
		
		view.addObject("remote", remoteBiz.selectMaxTimeByDeviceNo(device_no,"stormwater"));
		Map<Integer, String> startTypeMap = dictionaryBiz.queryDictionaryByParent(
				 DictionaryEnums.START_MODE.getCode());
		view.addObject("startTypeMap", startTypeMap);
		Map<Integer, String> remoteTypeMap = dictionaryBiz.queryDictionaryByParent(
				 DictionaryEnums.REMOTE_TYPE.getCode());
		view.addObject("remoteTypeMap", remoteTypeMap);
		Map<Integer, String> samplingTypeMap = dictionaryBiz.queryDictionaryByParent(
				 DictionaryEnums.CONCENTRATION_SAMPLING.getCode());
		view.addObject("samplingTypeMap", samplingTypeMap);
		return view;
	}

	/*@RequestMapping(value = "/deviceNoChange")
	public @ResponseBody String companyNameChange( String[] arr,String remoteType) {
		String str=StringUtils.join(arr, ",");
		 String remoteDeviceNo=","+str+",";
		 Remotecontrol remotecontrol=remoteBiz.selectMaxTimeByDeviceNo(remoteDeviceNo, remoteType);
		if(remotecontrol!=null){
			if(remoteType.equals("discharge")){
			Map<String, Object> map=new HashMap<String, Object>();
			map.put("valveSetting",remotecontrol.getValveSetting());
			map.put("samplingMode", remotecontrol.getSamplingMode());
			map.put("singleSamplingQuantity", remotecontrol.getSingleSamplingQuantity());
			map.put("samplingInterval", remotecontrol.getSamplingInterval());
			map.put("cumulativeFlow", remotecontrol.getCumulativeFlow());
			map.put("samplingTotaltime", remotecontrol.getSamplingTotaltime());
			map.put("StartupMode", remotecontrol.getStartupMode());
			map.put("delayed", remotecontrol.getDelayed());
			return JSONObject.toJSONString(map);
		}else if(remoteType.equals("rainwater")){
			Map<String, Object> map=new HashMap<String, Object>();
			map.put("valveSetting",remotecontrol.getValveSetting());
			map.put("rainwaterOpenLength", remotecontrol.getRainwaterOpenLength());
			map.put("singleSamplingQuantity", remotecontrol.getSingleSamplingQuantity());
			map.put("samplingInterval", remotecontrol.getSamplingInterval());
			map.put("samplingTotaltime", remotecontrol.getSamplingTotaltime());
			map.put("StartupMode", remotecontrol.getStartupMode());
			return JSONObject.toJSONString(map);
		}else if(remoteType.equals("stormwater")){
			Map<String, Object> map=new HashMap<String, Object>();
			map.put("samplingMode",remotecontrol.getSamplingMode());
			map.put("delayed", remotecontrol.getDelayed());
			map.put("cumulativeFlow", remotecontrol.getCumulativeFlow());
			map.put("singleSamplingQuantity", remotecontrol.getSingleSamplingQuantity());
			map.put("samplingInterval", remotecontrol.getSamplingInterval());
			map.put("samplingTotaltime", remotecontrol.getSamplingTotaltime());
			map.put("StartupMode", remotecontrol.getStartupMode());
			return JSONObject.toJSONString(map);
		}
		}
		return null;
		
	}*/
	//采样仪
	@RequestMapping(value = "/samplingSend")
	@ResponseBody
	public CommonResult samplingSend(Remotecontrol remotecontrol) {
		CommonResult result = remoteBiz.samplingSetSend(remotecontrol);
		return result;

	}


	//阀门
	@RequestMapping(value = "/waitResponse/{waitType}")
	@ResponseBody
	public CommonResult waitResponse(Remotecontrol remotecontrol,@PathVariable(value = "waitType") String waitType) {
		CommonResult result = remoteBiz.waitResponse(remotecontrol,waitType);

		return result;
	}


	//阀门
	@RequestMapping(value = "/valveSend")
	@ResponseBody
	public CommonResult valveSend(Remotecontrol remotecontrol) {
		CommonResult result = remoteBiz.sendSwitchValve(remotecontrol);
		
		return result;
	}
	//复位
	@RequestMapping(value = "/resetSampling")
	@ResponseBody
	public CommonResult resetSampling(Remotecontrol remotecontrol) {
		CommonResult result = remoteBiz.Reset(remotecontrol);
		return result;
	}
	
	@RequestMapping(value = "/fluggasDischargeRemote")
	public ModelAndView fluggasDischargeRemote() {
		ModelAndView view = new ModelAndView("RemoteList/fluggasDischargeRemote");
		view.addObject("companyList", companyBiz.getList(DictionaryEnums.DISCHARGE));
		view.addObject("fluggasDischargeList", this.fluggasDischargeBiz.getAll());
		view.addObject("isRemoteList", true);
		view.addObject("remote",remoteBiz.selectMaxTime("fluggasDischarge") );
		return view;
	}

	@RequestMapping(value = "/radioFluggasDischargeRemote/{device_no}")
	public ModelAndView radioFluggasDischargeRemote(@PathVariable(value = "device_no") String device_no){
		ModelAndView view = new ModelAndView("RemoteList/fluggasDischargeRemote");
		String remoteDeviceNo = "," + device_no + ",";
		view.addObject("companyList", companyBiz.getList(DictionaryEnums.DISCHARGE));
		view.addObject("fluggasDischarge", this.fluggasDischargeBiz.getByDeviceNo(device_no));
		view.addObject("remote", remoteBiz.selectMaxTimeByDeviceNo(remoteDeviceNo, "fluggasDischarge"));
		return view;
	}

}
